<template>
  <div>
    <div class="back">
      <!-- 导航栏 -->
      <!-- <van-nav-bar>
        <template #left>
          <i class="iconfont" @click="btn">&#xe637;</i>
        </template>
      </van-nav-bar> -->
      <NavBar path="/Home/Find"></NavBar>
      <div class="center">
        <div class="title">
          您好, 请登录
        </div>
        <div class="body">
          <van-form ref="form" @submit="onSubmit">
            <van-cell-group>
              <van-field
                placeholder="请输入手机号"
                v-model="form.mobile"
                :rules="rules.mobile"
                name="phone"
              >
                <template #left-icon>
                  <i class="iconfont">&#xe60b;</i>
                </template>
              </van-field>
            </van-cell-group>
            <van-cell-group>
              <van-field
                placeholder="请输入验证码"
                v-model="form.code"
                :rules="rules.code"
              >
                <template #left-icon>
                  <i class="iconfont">&#xe60c;</i>
                </template>
                <template #button>
                  <van-button
                    size="mini"
                    @click="getCode"
                    :disabled="stop"
                    :class="{ bg: stop }"
                    >{{ showCode }}</van-button
                  >
                </template>
              </van-field>
            </van-cell-group>
            <div class="footer">
              登录即同意<a href="javascript:0">《用户使用协议》</a>和<a
                href="javascript:0"
                >《隐私协议》</a
              >
              <van-button round color="#e40137" size="large">确定</van-button>
            </div>
          </van-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getCodeApi, postUserCode } from '@/api/Login.js'
import { setToken } from '@/utils/token.js'
import { mapMutations } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      stop: false,
      timeNum: 6,
      showCode: '发送验证码',
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号不合法',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'onBlur' },
          {
            pattern: /^\d{4}$/,
            message: '验证码长度为4位',
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    btn () {
      this.$router.push('/Home/Find')
    },
    ...mapMutations(['setUserInfo', 'setIsLogin']),
    // 获取验证码
    getCode () {
      // 校验是否填入手机号
      this.$refs.form.validate(['phone']).then(async response => {
        // 发起请求的同时按钮倒计时
        this.stop = true
        this.showCode = `6s后重试`
        var _Interval = setInterval(() => {
          this.timeNum--
          this.showCode = `${this.timeNum}s后重试`
          if (this.timeNum <= 0) {
            clearInterval(_Interval)
            this.timeNum = 6
            this.showCode = `发送验证码`
            this.stop = false
          }
        }, 1000)

        // 获取验证码前等待时间显示加载中
        this.$toast.loading({
          message: '正在获取验证码'
          // duration: 3000
        })

        // 发起请求获取验证码
        const res = await getCodeApi(this.form.mobile)

        if (res.code === 200) {
          // console.log(res)
          this.$toast.success(res.data)
          this.form.code = res.data
        } else {
          this.$toast.success(res.message)
        }
      })
    },
    // 表单提交
    async onSubmit () {
      this.$toast.loading({
        message: '正在登录',
        type: 'loading'
      })
      // 发起请求登录
      // try catch的用法 , try里面的代码有错误 , 就会执行catch , 可以用来判断执行过程中代码是否发生错误
      try {
        const res = await postUserCode(this.form)
        // console.log(res)
        // 保存返回的token
        setToken(res.data.jwt)
        // 保存用户信息到state
        this.setUserInfo(res.data.user)
        // 保存登录状态到state
        this.setIsLogin(true)
        // 跳转到我的信息
        // console.log(this.$route)
        const _redirect = this.$route.query._redirect
        if (_redirect) {
          this.$router.push(_redirect)
        } else {
          this.$router.push('/Home/My')
        }
        // 跳转后清除轻提示
        // this.$toast.clear()
        this.$toast.success({
          message: '欢迎'
        })
      } catch {
        // console.log(err)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.back {
  // /deep/ .van-nav-bar__left {
  //   padding-left: 0;
  //   i.iconfont {
  //     font-size: 45px;
  //   }
  // }
  .center {
    padding: 50px 15px 0 15px;
    .title {
      font-size: 18px;
    }
    .body {
      margin-top: 63px;
      .iconfont {
        margin-right: 12px;
      }
      .van-hairline--top-bottom::after,
      .van-hairline-unset--top-bottom::after {
        border-width: 0 0 2px 0;
      }
      .van-button--default {
        border: 0;
        font-size: 16px;
        color: @color;
      }
      .van-cell {
        padding-right: 0;
      }
    }
  }
  // 动态绑定验证码按钮点击倒计时后颜色
  .bg {
    color: #d8cdcc !important;
  }
  .footer {
    font-size: 12px;
    padding-top: 15px;
    a {
      color: #00b8d4;
    }
    .van-button {
      margin-top: 44px;
    }
  }
}
</style>
